package com.hospital.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.common.R;
import com.hospital.pojo.Doctor;
import com.hospital.service.DoctorService;
import com.hospital.utils.GlobalUserUtil;
import com.hospital.utils.ShiroMd5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.print.Doc;
import java.util.List;


/**
 * @Author:晓风残月Lx
 * @Date: 2022/11/19 11:02
 */
@Controller
@Slf4j
@RequestMapping("/hospital")
public class DoctorController {
    @Resource
    private DoctorService doctorService;

    @ResponseBody
    @RequestMapping("/doctors")
    public String doctors() {
        return "/doctors";
    }

    @ResponseBody
    @RequestMapping("/add-doctor")
    public String addDoctor() {
        return "/add-doctor";
    }

    @RequestMapping("/update-doctor")
    public String updateDoctor() {
        return "/update-doctor";
    }

    @ResponseBody
    @PostMapping("/doctors/list")
    public R list(@RequestBody String req){
        long pageNo = Long.parseLong(JSONObject.parseObject(req).get("page_num").toString());
        long pageSize =Long.parseLong(JSONObject.parseObject(req).get("page_size").toString());
        String searchName = JSONObject.parseObject(req).get("search_name").toString() ;

        Page<Doctor> doctorPage = new Page<>(pageNo,pageSize);
        Page<Doctor> doctorList = null;
        if (searchName == null || searchName.equals("")){
            doctorList = doctorService.getList(doctorPage);
        }else{
            doctorList= doctorService.getListBySearchName(doctorPage,searchName);
        }
        if (doctorList == null){
            return R.error("数据库除了问题，请稍后重试！");
        }
        return R.success(doctorList);
    }

    @ResponseBody
    @DeleteMapping ("/doctors/delete")
    public R delete(@RequestParam("id") Integer id){
        if (doctorService.deleteById(id)){
            return R.success(1,"删除成功！");
        }
        return R.error("删除失败，请稍后重试！");
    }

    // 添加
    @ResponseBody
    @PostMapping("/doctors/add")
    public R saveOrUpdate(@RequestBody(required = true) Doctor doctor){
        if (doctor == null){
            return R.error("请填写完整！");
        }
        doctor.setPassword(ShiroMd5Util.SysMd5(doctor.getUserName(),doctor.getPassword()));
        if (doctorService.save(doctor)) {
            return R.success(1,"添加成功！");
        }
        return R.error("添加失败！");
    }


    // 修改状态
    @ResponseBody
    @PostMapping("/doctors/changeStatus")
    public R changeStatus (@RequestBody String req){
        Integer id = Integer.valueOf(JSONObject.parseObject(req).getString("id")) ;
        Integer status = Integer.valueOf(JSONObject.parseObject(req).getString("status"));
        int data = doctorService.changeStatus(id,status);
        if (data == 0 || data == 1) {
            return R.success(data,"修改成功!");
        }
        return R.error("修改失败!");
    }

    // 更新操作
    @ResponseBody
    @PutMapping("/doctors/update")
    public R update(@RequestBody(required = true) Doctor doctor){
        if (doctorService.updateDoc(doctor)) {
            if (doctor.getUserName().equals(GlobalUserUtil.getDoctor())){
                return R.success(1,"修改成功！");
            }else{
                return R.success(0,"修改成功！");
            }

        }else {
            return R.error("修改失败!");
        }
    }

    // 更新自己信息操作
    @ResponseBody
    @PutMapping("/doctors/updatePerson")
    public R updatePerson(@RequestBody String req){
        Long id = JSONObject.parseObject(req).getLong("id");
        String username = JSONObject.parseObject(req).getString("username");
        String password = JSONObject.parseObject(req).getString("password");
        String realname = JSONObject.parseObject(req).getString("realname");
        String image = JSONObject.parseObject(req).getString("image");
        boolean flag = doctorService.updatePerson(id,username,password,realname,image);
        if (flag){
            return R.success(1,"修改成功！");
        }
        return R.error("修改失败！");
    }

    // 查询一个医生信息
    @ResponseBody
    @GetMapping("/doctors/getOne/{id}")
    public R selectOne(@PathVariable("id") Integer id){
        Doctor doctor = doctorService.getOneById(id);
        return R.success(doctor);
    }

}
